<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input :placeholder="'请输入' + tempDesc.name" v-model="listQuery.name" style="width: 200px;margin-right: 20px" class="filter-item"
                @keyup.enter.native="handleFilter"/>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        {{ $t('table.search') }}
      </el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit"
                 @click="handleCreate">{{ $t('table.add') }}
      </el-button>
    </div>

    <el-table
      v-loading="listLoading"
      :key="tableKey"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;">

      <el-table-column :label="tempDesc.name" width="150px" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="tempDesc.qrUrl"  show-overflow-tooltip width="100px">
        <template slot-scope="scope">
          <template v-if="!!scope.row.qrUrl">
            <el-popover
              placement="top-start"
              trigger="hover">
            <span>
              <img :src="scope.row.qrUrl" alt="" style="width: 200px;height: 200px">
            </span>
              <span slot="reference">
              <img :src="scope.row.qrUrl" alt="" style="width: 50px;height: 50px">
            </span>
            </el-popover>
          </template>
        </template>
      </el-table-column>
      <el-table-column :label="tempDesc.account" width="130px" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ scope.row.account }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="tempDesc.appid" width="150px" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ scope.row.appid }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="tempDesc.appsecret" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ scope.row.appsecret }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="tempDesc.token" width="100px" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ scope.row.token }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="tempDesc.url" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ scope.row.url }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="tempDesc.aeskey" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ scope.row.aeskey }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="tempDesc.createTime" width="160px" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="tempDesc.updateTime" width="160px" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ scope.row.updateTime }}</span>
        </template>
      </el-table-column>

      <el-table-column :label="$t('table.actions')" align="center" width="280" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">{{ $t('table.edit') }}</el-button>
          <el-button type="danger" size="mini" @click="handleDelete(scope.row)">{{ $t('table.delete') }}</el-button>
          <el-button type="primary" size="mini" style="width: 110px;margin-left: 10px" @click="generateQRUrl(scope.row)">生成二维码图片</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit"
                @pagination="getList"/>

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="right" label-width="120px"
               style="width: 400px; margin-left:50px;">
        <el-form-item :label="tempDesc.name" prop="name">
          <el-input v-model="temp.name" :placeholder="'请输入' + tempDesc.name"/>
        </el-form-item>
        <el-form-item :label="tempDesc.account" prop="account">
          <el-input v-model="temp.account" :placeholder="'请输入' + tempDesc.account"/>
        </el-form-item>
        <el-form-item :label="tempDesc.appid" prop="appid">
          <el-input v-model="temp.appid" :placeholder="'请输入' + tempDesc.appid"/>
        </el-form-item>
        <el-form-item :label="tempDesc.appsecret" prop="appsecret">
          <el-input v-model="temp.appsecret" :placeholder="'请输入' + tempDesc.appsecret"/>
        </el-form-item>
        <el-form-item :label="tempDesc.token" prop="token">
          <el-input v-model="temp.token" :placeholder="'请输入' + tempDesc.token"/>
        </el-form-item>
        <el-form-item :label="tempDesc.url" prop="url">
          <el-input v-model="temp.url" :placeholder="'请输入' + tempDesc.url">
          </el-input>
          <span style="font-size: 14px;color: #8492a6">http://{域名}/api/wx/portal/{appid}</span>
        </el-form-item>
        <el-form-item :label="tempDesc.aeskey" prop="aeskey">
          <el-input v-model="temp.aeskey" :placeholder="'请输入' + tempDesc.aeskey"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button @click="dialogFormVisible = false">{{ $t('table.cancel') }}</el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">{{ $t('table.confirm') }}
        </el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  import {fetchList, createRow, updateRow, deleteRow,generateQRUrl} from '@/api/wx-account'
  import waves from '@/directive/waves' // Waves directive
  import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

  //常量title
  const tempDesc = {
    "id": "",
    "name": "公众号名称",
    "account": "公众号ID",
    "appid": "AppID",
    "appsecret": "AppSecret",
    "url": "URL",
    "token": "Token",
    "aeskey": "AesKey",
    "qrUrl":"二维码",
    "createTime": "创建时间",
    "updateTime": "更新时间"
  };
  export default {
    //name: 'ComplexTable',
    components: {Pagination},
    directives: {waves},
    filters: {},
    data() {
      return {
        tableKey: 0,
        list: null,
        total: 0,
        listLoading: true,
        listQuery: {
          page: 1,
          limit: 10,
          name: ""
        },
        showReviewer: false,
        temp: {
          "id": "",
          "name": "",
          "account": "",
          "appid": "",
          "appsecret": "",
          "url": "",
          "token": "",
          "aeskey": "",
          "qrUrl":""
        },
        tempDesc: tempDesc,
        dialogFormVisible: false,
        dialogStatus: '',
        textMap: {
          update: '编辑',
          create: '增加'
        },
        rules: {
          name: [{required: true, message: '请输入' + tempDesc.name, trigger: 'blur'}],
          account: [{required: true, message: '请输入' + tempDesc.account, trigger: 'blur'}],
          appid: [{required: true, message: '请输入' + tempDesc.appid, trigger: 'blur'}],
          appsecret: [{required: true, message: '请输入' + tempDesc.appsecret, trigger: 'blur'}],
          url: [{required: true, message: '请输入' + tempDesc.url, trigger: 'blur'}],
          token: [{required: true, message: '请输入' + tempDesc.token, trigger: 'blur'}],
          aeskey: [{required: true, message: '请输入' + tempDesc.aeskey, trigger: 'blur'}]
        }
      }
    },
    created() {
      this.getList()
    },
    methods: {
      getList() {
        this.listLoading = true
        fetchList(this.listQuery).then(response => {
          this.list = response.data.list
          this.total = response.data.total

          // Just to simulate the time of the request
          setTimeout(() => {
            this.listLoading = false
          }, 1.5 * 1000)
        })
      },
      handleFilter() {
        this.listQuery.page = 1
        this.getList()
      },
      resetTemp() {
        this.temp = {
          "id": "",
          "name": "",
          "account": "",
          "appid": "",
          "appsecret": "",
          "url": "",
          "baseAPI":"",
          "token": "",
          "aeskey": ""
        }
      },
      handleCreate() {
        this.resetTemp()
        this.dialogStatus = 'create'
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },
      createData() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            createRow(this.temp).then(() => {
              this.getList();
              this.dialogFormVisible = false
              this.$notify({
                title: '成功',
                message: '创建成功',
                type: 'success',
                duration: 2000
              })
            })
          }
        })
      },
      handleUpdate(row) {
        this.temp = Object.assign({}, row) // copy obj
        this.dialogStatus = 'update'
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },
      updateData() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            const tempData = Object.assign({}, this.temp)
            updateRow(tempData).then(() => {
              this.getList();
              this.dialogFormVisible = false
              this.$notify({
                title: '成功',
                message: '更新成功',
                type: 'success',
                duration: 2000
              })
            })
          }
        })
      },
      handleDelete(row) {
        this.$confirm('此操作将永久删除, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            var params = {"id": row.id};
            deleteRow(params).then(() => {
              this.$notify({
                title: '成功',
                message: '删除成功',
                type: 'success',
                duration: 2000
              });
              this.getList();
            });
          });
      },
      generateQRUrl(row){
        this.$confirm('确定生成二维码, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
            const tempData = Object.assign({}, row);
            generateQRUrl(tempData).then(() => {
              this.getList();
              this.$notify({
                title: '成功',
                message: '生成二维码成功',
                type: 'success',
                duration: 2000
              })
            })
          });

      }
    }

  }
</script>
